home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / resources.pak / Unnamed File 000078.txt < prev    next >
Text File  |  2013-04-03  |  5KB  |  191 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. /**
  6.  * @fileoverview This is a table column model
  7.  */
  8. cr.define('cr.ui.table', function() {
  9.   /** @const */ var EventTarget = cr.EventTarget;
  10.   /** @const */ var Event = cr.Event;
  11.  
  12.   /**
  13.    * A table column model that wraps table columns array
  14.    * This implementation supports widths in percents.
  15.    * @param {!Array<cr.ui.table.TableColumn>} columnIds Array of table columns.
  16.    * @constructor
  17.    * @extends {EventTarget}
  18.    */
  19.   function TableColumnModel(tableColumns) {
  20.     this.columns_ = [];
  21.     for (var i = 0; i < tableColumns.length; i++) {
  22.       this.columns_.push(tableColumns[i].clone());
  23.     }
  24.   }
  25.  
  26.   var MIMIMAL_WIDTH = 10;
  27.  
  28.   TableColumnModel.prototype = {
  29.     __proto__: EventTarget.prototype,
  30.  
  31.     /**
  32.      * The number of the columns.
  33.      * @type {number}
  34.      */
  35.     get size() {
  36.       return this.columns_.length;
  37.     },
  38.  
  39.     /**
  40.      * Returns id of column at the given index.
  41.      * @param {number} index The index of the column.
  42.      * @return {string} Column id.
  43.      */
  44.     getId: function(index) {
  45.       return this.columns_[index].id;
  46.     },
  47.  
  48.     /**
  49.      * Returns name of column at the given index. Name is used as column header
  50.      * label.
  51.      * @param {number} index The index of the column.
  52.      * @return {string} Column name.
  53.      */
  54.     getName: function(index) {
  55.       return this.columns_[index].name;
  56.     },
  57.  
  58.     /**
  59.      * Sets name of column at the given index.
  60.      * @param {number} index The index of the column.
  61.      * @param {string} Column name.
  62.      */
  63.     setName: function(index, name) {
  64.       if (index < 0 || index >= this.columns_.size - 1)
  65.         return;
  66.       if (name != this.columns_[index].name)
  67.         return;
  68.  
  69.       this.columns_[index].name = name;
  70.       cr.dispatchSimpleEvent('change');
  71.     },
  72.  
  73.     /**
  74.      * Returns width (in percent) of column at the given index.
  75.      * @param {number} index The index of the column.
  76.      * @return {string} Column width in pixels.
  77.      */
  78.     getWidth: function(index) {
  79.       return this.columns_[index].width;
  80.     },
  81.  
  82.     /**
  83.      * Check if the column at the given index should align to the end.
  84.      * @param {number} index The index of the column.
  85.      * @return {boolean} True if the column is aligned to end.
  86.      */
  87.     isEndAlign: function(index) {
  88.       return this.columns_[index].endAlign;
  89.     },
  90.  
  91.     /**
  92.      * Sets width of column at the given index.
  93.      * @param {number} index The index of the column.
  94.      * @param {number} Column width.
  95.      */
  96.     setWidth: function(index, width) {
  97.       if (index < 0 || index >= this.columns_.size - 1)
  98.         return;
  99.  
  100.       width = Math.max(width, MIMIMAL_WIDTH);
  101.       if (width == this.columns_[index].width)
  102.         return;
  103.  
  104.       this.columns_[index].width = width;
  105.       cr.dispatchSimpleEvent(this, 'resize');
  106.     },
  107.  
  108.     /**
  109.      * Returns render function for the column at the given index.
  110.      * @param {number} index The index of the column.
  111.      * @return {Function(*, string, cr.ui.Table): HTMLElement} Render function.
  112.      */
  113.     getRenderFunction: function(index) {
  114.       return this.columns_[index].renderFunction;
  115.     },
  116.  
  117.     /**
  118.      * Sets render function for the column at the given index.
  119.      * @param {number} index The index of the column.
  120.      * @param {Function(*, string, cr.ui.Table): HTMLElement} Render function.
  121.      */
  122.     setRenderFunction: function(index, renderFunction) {
  123.       if (index < 0 || index >= this.columns_.size - 1)
  124.         return;
  125.       if (renderFunction !== this.columns_[index].renderFunction)
  126.         return;
  127.  
  128.       this.columns_[index].renderFunction = renderFunction;
  129.       cr.dispatchSimpleEvent(this, 'change');
  130.     },
  131.  
  132.     /**
  133.      * Render the column header.
  134.      * @param {number} index The index of the column.
  135.      * @param {cr.ui.Table} Owner table.
  136.      */
  137.     renderHeader: function(index, table) {
  138.       var c = this.columns_[index];
  139.       return c.headerRenderFunction.call(c, table);
  140.     },
  141.  
  142.     /**
  143.      * The total width of the columns.
  144.      * @type {number}
  145.      */
  146.     get totalWidth() {
  147.       var total = 0;
  148.       for (var i = 0; i < this.size; i++) {
  149.         total += this.columns_[i].width;
  150.       }
  151.       return total;
  152.     },
  153.  
  154.     /**
  155.      * Normalizes widths to make their sum 100%.
  156.      */
  157.     normalizeWidths: function(contentWidth) {
  158.       if (this.size == 0)
  159.         return;
  160.       var c = this.columns_[0];
  161.       c.width = Math.max(10, c.width - this.totalWidth + contentWidth);
  162.     },
  163.  
  164.     /**
  165.      * Returns default sorting order for the column at the given index.
  166.      * @param {number} index The index of the column.
  167.      * @return {string} 'asc' or 'desc'.
  168.      */
  169.     getDefaultOrder: function(index) {
  170.       return this.columns_[index].defaultOrder;
  171.     },
  172.  
  173.     /**
  174.      * Returns index of the column with given id.
  175.      * @param {string} id The id to find.
  176.      * @return {number} The index of column with given id or -1 if not found.
  177.      */
  178.     indexOf: function(id) {
  179.       for (var i = 0; i < this.size; i++) {
  180.         if (element.id == id)
  181.           return i;
  182.       }
  183.       return -1;
  184.     },
  185.   };
  186.  
  187.   return {
  188.     TableColumnModel: TableColumnModel
  189.   };
  190. });
  191.